﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.PhongMau
{
    public partial class DanhSachSanPhamPopup : Form
    {
        public DanhSachSanPhamPopup()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            gridSanPham.DataSource = DataAll.SelectAll("PhongMau_SanPham", "*", "WHERE TrangThai = " + Convert.ToInt32(TrangThai.PhongMau_ChoDuyet));
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvSanPham.BestFitColumns();
            gvSanPham.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvSanPham.OptionsBehavior.Editable = false;
            gvSanPham.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvSanPham.OptionsSelection.MultiSelect = true;
            gvSanPham.OptionsView.ShowGroupPanel = false;
            gvSanPham.OptionsView.ColumnAutoWidth = false;
            gvSanPham.OptionsView.ShowFooter = true;
            gvSanPham.OptionsFind.AlwaysVisible = true;
            gvSanPham.Columns["NguoiDangKy"].Fixed = FixedStyle.Left;
        }

        private void gvSanPham_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "TrangThai")
            {
                e.DisplayText = Common.HienThiTrangThai(Convert.ToInt32(e.Value));
            }
        }

        private void btDuyetSP_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvSanPham.DataSource;
            string a = gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.PhongMau_ChoDuyet).ToString();
            if (a == b)
            {

                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn duyệt sản phẩm.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("PhongMau_SanPham", "TrangThai =" + Convert.ToInt32(TrangThai.PhongMau_DuyetSanPham) + ",NgayGui ='" + DateTime.Now.ToString("MM/dd/yyyy") + "' WHERE IdSanPham =" + dataview[gvSanPham.FocusedRowHandle]["IdSanPham"]);

                    MessageBox.Show(message.Equals("success") ? "Duyệt sản phẩm thành công" : message, "Message",
                                        MessageBoxButtons.OK,
                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    if (message.Equals("success"))
                    {
                        DataAll.InsertAll("HanhTrinh", dataview[gvSanPham.FocusedRowHandle]["MaHang"]
                            + "," + Convert.ToInt32(PhongBan.PhongMau)
                            + "," + dataview[gvSanPham.FocusedRowHandle]["SoChungTu"]
                            + "," + Convert.ToInt32(TrangThai.PhongMau_DuyetSanPham)
                            + "," + dataview[gvSanPham.FocusedRowHandle]["NguoiDangKy"]
                            + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                            + "," + dataview[gvSanPham.FocusedRowHandle]["SoLuong"]
                            + "," + Convert.ToDateTime(dataview[gvSanPham.FocusedRowHandle]["TuNgay"]).ToString("MM/dd/yyyy")
                            + "," + Convert.ToDateTime(dataview[gvSanPham.FocusedRowHandle]["DenNgay"]).ToString("MM/dd/yyyy"));
                    }
                    LoadData();
                }
            }
        }

        private void btKhongDuyet_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvSanPham.DataSource;
            string a = gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "TrangThai").ToString();
            string b = Convert.ToInt32(TrangThai.PhongMau_ChoDuyet).ToString();
            if (a == b)
            {

                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn hủy duyệt.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    string message = DataAll.Update("PhongMau_SanPham", "TrangThai =" + Convert.ToInt32(TrangThai.PhongMau_KhongDuyet) + ",NgayGui ='" + DateTime.Now.ToString("MM/dd/yyyy") + "' WHERE IdSanPham =" + dataview[gvSanPham.FocusedRowHandle]["IdSanPham"]);

                    MessageBox.Show(message.Equals("success") ? "Hủy duyệt thành công" : message, "Message",
                                        MessageBoxButtons.OK,
                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    LoadData();
                }
            }
        }

        private void gvSanPham_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "DenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }
    }
}
